package team.gl.nio.cln;

import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.Vector;
import java.util.concurrent.TimeUnit;

import com.richman.controller.DecreaseRatioClient;
import com.richman.ps38.PS38Thread;
import com.richman.ps38.entity.Event;
import com.richman.ps38.entity.SendData;
import com.richman.utils.Common;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import team.gl.nio.cmn.Bag;

public class ClientHandler extends ChannelInboundHandlerAdapter {  
	
	long lastreconnecttime = 0;
	
	
	
	public static ChannelHandlerContext ctx_s = null;	

    @Override  
    public void channelRead(ChannelHandlerContext ctx, Object msg)  
            throws Exception {     
    	Bag us = (Bag)msg;
    	
    	SimpleDateFormat dfSec = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	
    	if(us.getReq().equals("leaguesEvents")){
    		
    		
    		
    		
    		DecreaseRatioClient.mainWnd.updateMainWndStatusText(1, "与服务器连接正常");
    		
    		
    			
    		//System.out.println("recv start ----------------------------------------");

//    			
//    			for(SendData g: us.senddatas){
//    				
//    				//System.out.println(g.teamh  + "vs" + g.teamc);
//    				
//    				Event event = g.event;
//    				
//    				//System.out.println(event.homeTeam + "-vs-" + event.awayTeam);
//    				
//    				
//    				
//    				
//    			}
//    			
//    			
//
//				if(SpecialServer.ps38Thread != null && System.currentTimeMillis() - PS38Thread.refreshTime > 120*1000 ) {
//					
//					Common.logOut("PS38Thread 已长时间未更新,线程可能已经停止运行!!!");
//
//					SpecialServer.ps38Thread.interrupt();
//					
//					SpecialServer.betCtrMng.updatePS38StatusCode(-1); 
//
//					PS38Thread.refreshTime = System.currentTimeMillis();
//					
//				}
//
//    			
//    			
//    			
//    			if(SpecialServer.betCtrMng != null) {
//    				SpecialServer.betCtrMng.updateBetCentralTable(us.senddatas, null);
//    			}
//    			
//    			if(SpecialServer.useForOriginal==false) {
//					SpecialServer.betCtrMng.updateDanshiPankouAndDaxiao(us.hgevents);
//				}
    		
    		
    		
    			
    			//System.out.println("recv end----------------------------------------");
    			
    			DecreaseRatioClient.mainWnd.setForbidLeagues(us.getForbidLeagues());
    			
    			DecreaseRatioClient.mainWnd.setNegLeagues(us.getNegLeagues());
    			
    		}
    		
    		
    	}

    
    
    
    
    
	@Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("--- Server is active ---");
        ctx_s = ctx;
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("--- Server is inactive ---");
        NettyClient.channel = null;
        // 10s 之后尝试重新连接服务器
        System.out.println("2s 之后尝试重新连接服务器...");
        
        
        
        NettyClient.group.schedule(new Runnable() {  
            
            @Override  
          
            public void run() {  
                while(NettyClient.connect() == false) {
                	
                	try{  					
    					Thread.sleep(5000);
    					
    				}catch(Exception e){
    					e.printStackTrace();
    				} 
                }
                
                
                System.out.println("重新连接成功");
            }  
          
          }, 200L, TimeUnit.MILLISECONDS);  
          
        super.channelInactive(ctx);
        
        
        
       // NettyClient.sendRequest(new Bag("request"));
    }
    
    
    
    
    
    
    

}